package Twx0429;

import Twx0429.User;

import java.util.ArrayList;
import java.util.List;

public class UserService {
    UserStorage storage = new UserStorage();
    List<User> userList = storage.loadUsers();

    public int login(String name, String pwd) {
        for(int i = 0; i < userList.size(); i++){
            User user = userList.get(i);
            if (user.getName().equals(name)){
                return user.login(pwd);
            }
        }
        return 3;
    }

    public int register(String name, String pwd){
        for(int i = 0; i < userList.size(); i++){
            User user = userList.get(i);
            if (user.getName().equals(name)){
               return 0;
            }
        }
        if(name.length() < 5 || name.length() >12){
            return 1;
        } else if (pwd.length() < 6 || pwd.length() > 12) {
            return 2;
        }
        userList.add(new User(name, pwd, false));
        return 3;
    }
}